export class TslNvrCmd {
  constructor(id) {
    this.id = id;
  }
  getOCXInstance() {
    return window[this.id];
  }
  OCXCmd(cmd, params) {
    const ins = this.getOCXInstance();
    if (!ins) {
      return console.warn('OCXInstance is not available');
    }
    if (typeof ins.CtrlCmd !== 'function') {
      return console.warn('OCXInstance.CtrlCmd is not a function');
    }
    try {
      return ins.CtrlCmd(cmd, params);
    } catch (e) {
      console.error('try OCXInstance.CtrlCmd: ' + cmd + 'field');
    }
  }
  /**
   * 创建视频播放窗口
   * @param {创建窗口类型nvr_realshow--实时，nvr_playback--回放} realshow
   */
  create({ realshow = true, viewnum = 1 }) {
    const playType = realshow ? 'nvr_realshow' : 'nvr_playback';
    const paramsField = realshow ? 'realparams' : 'playbackparams';
    const params = {
      login: {
        token: '123456',
        ip: '118.121.58.45',
        port: 17035,
        streamport: 17035,
      },
      [paramsField]: {
        viewnum: viewnum,
      },
    };
    return this.OCXCmd(
      'Create',
      JSON.stringify({
        type: 'plugin',
        plugin: playType,
        params: JSON.stringify(params),
      })
    );
  }
  replayVideo({ id, name, beginTime, endTime }) {
    this.OCXCmd(
      'openchn',
      JSON.stringify({
        id: id,
        name: name,
        time: beginTime,
      })
    );
    this.OCXCmd(
      'setTimeRegion',
      JSON.stringify({
        beginTime: beginTime,
        endTime: endTime,
      })
    );
    this.playWindow();
  }
  /**
   * 打开实时播放通道
   * @param {视频流id} hid
   * @param {设备名称} uname
   */
  playVideo(data = {}) {
    if (data && data.code) {
      const params = { id: data.code, name: data.name, moveNextFocus: 1 };
      this.OCXCmd('open', JSON.stringify(params));
    }
  }
  playVideos(params = []) {
    params.forEach((item) => {
      this.playVideo(item);
    });
  }
  /**
   * 分屏
   * @param {分屏数：1,4,6,9,16,25,36} num
   */
  splitScreen(num) {
    this.OCXCmd('split', JSON.stringify({ num: num }));
  }
  /**
   * 获取焦点窗口号
   */
  getfocusindex() {
    return this.OCXCmd('getfocusindex', {});
  }
  /**
   * 播放当前选中窗口
   */
  playWindow() {
    this.OCXCmd('play', { index: -1 });
  }
  /**
   * 全屏，会把整个电脑屏幕占满
   */
  fullWindow() {
    this.OCXCmd('full', {});
  }
  /**
   * 暂停视频
   */
  pauseViod() {
    this.OCXCmd('pause', { index: -1 });
  }
  /**
   * 停止视频
   */
  stopViod() {
    this.OCXCmd('stop', { index: -1 });
  }
  /**
   * 视频播放速度
   * @param {速度16、8、4、2、1、0.5、0.25、0.125、0.0625} rate
   */
  setrateViod(rate) {
    this.OCXCmd('setrate', { index: -1, rate: rate });
  }
  /**
   * 下载视频
   * @param {视频流id} hid
   * @param {开始时间(毫秒)} begintime
   * @param {结束时间（毫秒）} endtime
   */
  downloadViod(hid, begintime, endtime) {
    const params = {
      files: [{ id: hid, begintime: begintime, endtime: endtime }],
    };
    this.OCXCmd('download', params);
  }
  /**
   * 关闭通道
   */
  closechnViod() {
    this.OCXCmd('closechn', { index: -1 });
  }
  /**
   * 关闭所有通道
   */
  closeallchnViod() {
    this.OCXCmd('closeallchn', {});
  }
  /**
   * 设置显示比例
   * @param {比例 0:填充 1：自适应 2：4*3  3：16*9} percent
   */
  setwidthpercentViod(percent) {
    this.OCXCmd('setwidthpercent', { index: -1, percent: percent });
  }
  /**
   * 设置码流类型
   * @param {0:主码流 1：自动切换码流 2：子码流} streamtype
   */
  setstreamtypeViod(streamtype) {
    this.OCXCmd('setstreamtype', { index: -1, streamtype: streamtype });
  }
  /**
   * 停止转动
   */
  pantiltstopViod() {
    this.OCXCmd('ptz', { ptzcmd: 'pantiltstop', speed: 50 });
  }
  /**
   * 左上转动
   */
  leftupViod() {
    this.OCXCmd('ptz', { ptzcmd: 'leftup', speed: 50 });
  }
  /**
   * 正上转动
   */
  tiltupViod() {
    this.OCXCmd('ptz', { ptzcmd: 'tiltup', speed: 50 });
  }
  /**
   * 右上转动
   */
  rightupViod() {
    this.OCXCmd('ptz', { ptzcmd: 'rightup', speed: 50 });
  }
  /**
   * 左侧转动
   */
  panleftViod() {
    this.OCXCmd('ptz', { ptzcmd: 'panleft', speed: 50 });
  }
  /**
   * 回正转动
   */
  gotohomeViod() {
    this.OCXCmd('ptz', { ptzcmd: 'gotohome', speed: 50 });
  }
  /**
   * 右侧转动
   */
  panrightViod() {
    this.OCXCmd('ptz', { ptzcmd: 'panright', speed: 50 });
  }
  /**
   * 左下转动
   */
  leftdownViod() {
    this.OCXCmd('ptz', { ptzcmd: 'leftdown', speed: 50 });
  }
  /**
   * 正下转动
   */
  tiltdownViod() {
    this.OCXCmd('ptz', { ptzcmd: 'tiltdown', speed: 50 });
  }
  /**
   * 右下转动
   */
  rightdownViod() {
    this.OCXCmd('ptz', { ptzcmd: 'rightdown', speed: 50 });
  }
}
